package com.line.myprojectservice.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.line.myprojectservice.Mapper.NoteMapper;
import com.line.myprojectservice.utils.Pager;
import com.line.myprojectservice.dto.NoteDto;
import com.line.myprojectservice.po.Note;
import com.line.myprojectservice.service.NoteService;
import com.line.myprojectservice.utils.RedisUtil;
import com.line.myprojectservice.vo.NoteVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class NoteServiceImpl implements NoteService {

    @Resource
    NoteMapper noteMapper;

    @Override
    public Pager<NoteVo> findByPager(int page, int size) {
        Map<String, Object> params = new HashMap<>();
        params.put("page", (page - 1) * size);
        params.put("size", size);
        Pager<NoteVo> pager = new Pager<>();
        List<NoteVo> list = noteMapper.findByPager(params);
        pager.setRows(list);
        pager.setTotal(noteMapper.count());
        return pager;
    }

    @Override
    public void addNote(NoteDto noteDto) {
        Note note = new Note();
        // 定义id唯一标识
        note.setNoteId(IdUtil.fastSimpleUUID());
        note.setNoteTitle(noteDto.getNoteTitle());
        note.setNoteBody(noteDto.getNoteBody());
        note.setNoteAuthorId(noteDto.getNoteAuthorId());
        note.setNoteCreatetime(DateUtil.today());
        noteMapper.addNote(note);
    }


    @Override
    public Set<Object> getHotAnswer() {
        return RedisUtil.zReverseRange("HotAnswer", 0, 10);
    }


}
